/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.sec.content;

import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.Searchable;
import han.bifang.gwt.client.common.entity.Usergroup;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.sec.SecurityConstants;
import han.bifang.gwt.client.sec.SecurityContentConstants;
import han.bifang.gwt.client.sec.SecurityMessages;
import han.bifang.gwt.client.sec.content.group.CreateGroupContent;
import han.bifang.gwt.client.sec.content.group.GroupDetailContent;
import han.bifang.gwt.client.sec.content.group.GroupMgmtContent;
import han.bifang.gwt.client.sec.content.group.RemoveGroupContent;
import han.bifang.gwt.client.sec.content.user.CreateUserContent;

import java.util.HashMap;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class GroupTab extends Composite implements ContentChangeListener {

	private SecurityMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;
	
	private SimplePanel panel;
	
	public GroupTab(SecurityMessages cons,BifangGWTServiceAsync service, GwtContext context) {
		this.service = service;
		this.context = context;
		this.cons = cons;

		
		panel = new SimplePanel();
		
		GroupMgmtContent u = new GroupMgmtContent(cons,service, context);
		u.setContentChangeListener(this);
		panel.setWidget(u);
		initWidget(panel);
	}

	
	public void onContentChanged(String source, String dest, HashMap props) {
		if (source.equals(SecurityContentConstants.SEARCH_GROUP_RESULT) &&
				dest.equals(SecurityContentConstants.GROUP_DETAIL)) {
			String id = (String)props.get("id");
			context.put(SecurityConstants.SEARCH_PAGE, panel.getWidget());
			GroupDetailContent c = new GroupDetailContent(cons,service, context,id);
			c.setContentChangeListener(this);
			this.panel.setWidget(c);
			return;
		}
		
		if (source.equals(SecurityContentConstants.GROUP_DETAIL) &&
				dest.equals(SecurityContentConstants.SEARCH_GROUP_RESULT)) {
			//String id = (String)props.get("id");
			Searchable s = (Searchable)context.get(SecurityConstants.SEARCH_PAGE);
			s.refresh();
			 
			
			this.panel.setWidget((Widget)s);
			return;
		}
		
		if (source.equals(SecurityContentConstants.GROUP_DETAIL) &&
				dest.equals(SecurityContentConstants.REMOVE_GROUP)) {
			Usergroup i = (Usergroup)props.get("group");
			
			RemoveGroupContent c = new RemoveGroupContent(cons, service,  i.getId(),i.getGroupName());
			c.setContentChangeListener(this);
			 
			
			this.panel.setWidget(c);
			return;
		}
		
		if (source.equals(SecurityContentConstants.REMOVE_GROUP) &&
				dest.equals(SecurityContentConstants.GROUP_DETAIL)) {
			String id = (String)props.get("id");
			GroupDetailContent c = new GroupDetailContent(cons,service, context,id);
			c.setContentChangeListener(this);
			 
			
			this.panel.setWidget(c);
			return;
		}
		
		if (source.equals(SecurityContentConstants.REMOVE_GROUP) &&
				dest.equals(SecurityContentConstants.GROUP_MGMT)) {
			//String id = (String)props.get("id");
			Searchable s = (Searchable)context.get(SecurityConstants.SEARCH_PAGE);
			s.refresh();
			 
			
			this.panel.setWidget((Widget)s);
			return;
			
		}
		
		if (source.equals(SecurityContentConstants.GROUP_MGMT) &&
				dest.equals(SecurityContentConstants.CREATE_GROUP)) {
			CreateGroupContent c = new CreateGroupContent(cons,service, context);
			c.setContentChangeListener(this);
			 
			
			this.panel.setWidget(c);
			return;
			
		}
		if (source.equals(SecurityContentConstants.CREATE_GROUP) &&
				dest.equals(SecurityContentConstants.GROUP_DETAIL)) {
			CreateUserContent c = new CreateUserContent(cons,service, context);
			c.setContentChangeListener(this);
			 
			
			this.panel.setWidget(c);
			return;
			
		}
		
		if (source.equals(SecurityContentConstants.CREATE_GROUP) &&
				dest.equals(SecurityContentConstants.GROUP_MGMT)) {
			//String id = (String)props.get("id");
			Searchable s = (Searchable)context.get(SecurityConstants.SEARCH_PAGE);
			if(s != null){
				s.refresh();
				this.panel.setWidget((Widget)s);
			}else{
				GroupMgmtContent u = new GroupMgmtContent(cons,service, context);
				u.setContentChangeListener(this);
				panel.setWidget(u);
			}
			
			
			return;
			
		}
		
	}
	
	
}
